<?php

require_once("./lib/pluginbase.php");

class SavePlugin extends PluginBase
{
	// 2010-08-09 14:15:12 [INFO] Connected players: hillbillyal, Aquagene

	protected $players;

	function __construct()
	{
		if (function_exists("parent::__construct"))
			parent::__construct();
		echo "list\n";
	}

	function processRawMessage($time, $msg)
	{
		if (preg_match("/\\[INFO\\] Connected players: (([^,]*[, ]{0,2})*)$/", $msg, $matches) > 0)
		{
			preg_match_all("/[^, ]+/", $matches[1], $matches);
			$this->players = $matches[0];
		}
	}

	function processChatCommand($name, $cmd, $args)
	{
		if($cmd == "!list")
		{
			if($name == "SMP Server")
				fwrite(STDERR, "Players: " . implode(', ', $this->players) . "\n");
			else
			{
				echo "say Players: ";
				$i = 1;
				foreach($this->players as $users)
				{
					if($this->isAdmin($users))
						echo "@";
					echo $users;
					if(($i++) != count($this->players))
						echo ", ";
				}
				echo "\n";
			}
		}
	}

	function processUserJoin($name, $ip)
	{
		$this->players[] = $name;
	}

	function processUserPart($name, $reason)
	{
		foreach($this->players as $key => $value)
		{
			if ($value == $name)
				unset($this->players[$key]);
		}
		fwrite(STDERR, "Now there are " . count($this->players) . " players\n");
		if (count($this->players) == 0)
		{
			echo "stop\n";
		}
	}
}

registerPlugin("serversaver", new SavePlugin);

?>
